﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using text.Utils;

namespace text
{
    public partial class info : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitCity();
            }
           
        }
        public void InitCity()
        {
            string sql = ("select * from S_City");
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
            SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            ddl.DataSource = ds;
            ddl.DataTextField = "CityName";
            ddl.DataValueField = "CityID";
            ddl.DataBind();
            if (Session["User"] == null)
                return;
            DataRow userRow = (DataRow)Session["User"];
            int id = Convert.ToInt32(Request["id"]);

            if (id!= Convert.ToInt32(userRow["id"]))
            {
                DataSet ds1 = DbHelper.Query($@"select u.*,c.CityName as city_name  from users u inner join S_City c on c.CityID=u.city_id
                                           where u.id = "+id);
                DataRow dataRow = (DataRow)ds1.Tables[0].Rows[0];
                if (rb1.TabIndex == Convert.ToInt32(dataRow["gender"]))
                {
                    rb1.Checked = true;
                }
                else { rb2.Checked = true; }
                //登录人信息
                txtname.Text = dataRow["account"].ToString();
                txtpwd.Text = dataRow["pwd"].ToString();
                txtrepwd.Text = dataRow["pwd"].ToString();
                txtbirthday.Text = dataRow["birthday"].ToString();

                ddl.SelectedValue = dataRow["city_id"].ToString();
                Image1.ImageUrl = dataRow["img_url"].ToString();
                txtphone.Text = dataRow["phone"].ToString();
                txtemail.Text = dataRow["email"].ToString();
                txtqq.Text = dataRow["qq"].ToString();
                return;
            }
    
      
            if (rb1.TabIndex==Convert.ToInt32( userRow["gender"]))
            {
             rb1.Checked = true;
            }
            else { rb2.Checked = true; }
            //登录人信息
            txtname.Text = userRow["account"].ToString();
            txtpwd.Text= userRow["pwd"].ToString();
            txtrepwd.Text= userRow["pwd"].ToString();
            txtbirthday.Text= userRow["birthday"].ToString();
            
            ddl.SelectedValue= userRow["city_id"].ToString() ;
            Image1.ImageUrl= userRow["img_url"].ToString();
            txtphone.Text= userRow["phone"].ToString();
            txtemail.Text= userRow["email"].ToString();
            txtqq.Text= userRow["qq"].ToString();
        }
        protected void Button1_Click1(object sender, EventArgs e)
        {
            string img = FileUpload1.FileName;
            string name = ".jpg.png.gif.jfif";
            string exname = img.Substring(img.LastIndexOf('.'));
            if (FileUpload1.FileContent.Length > 1048576)
            {
                Response.Write("<script>alert('文件过大')</script>");
                return;
            }
            if (name.Contains(exname) == false)
            {
                Response.Write("<script>alert('文件不符合')</script>");
                return;
            }
            string path = Server.MapPath("images") + "\\" + img;
            FileUpload1.SaveAs(path);
            Image1.ImageUrl = "/images/" + img;
        }

        protected void btnregister_Click(object sender, EventArgs e)
        {

            string name = txtname.Text.Trim();
            string pwd = txtpwd.Text.Trim();
            string phone = txtphone.Text.Trim();
            string img = Image1.ImageUrl.Trim();
            int dl = Convert.ToInt32(ddl.SelectedValue);
            int sex;
            if (rb1.Checked)
            {
                sex = 0;
            }
            else
            {
                sex = 1;
            }
            string email = txtemail.Text.Trim();
            string qq = txtqq.Text.Trim();
            
            string birthday=txtbirthday.Text.Trim();
            DataRow userRow = (DataRow)Session["User"];
            int x = Convert.ToInt32(userRow["manager"]);
            int y = Convert.ToInt32(userRow["login_count"]);
            int z = Convert.ToInt32(userRow["type"]);
            int id = Convert.ToInt32(userRow["id"]);
            string sql = string.Format($@"update users set account='{name}',pwd='{pwd}', city_id={dl},gender={sex}, img_url='{img}',phone='{ phone}',email='{ email}',qq='{ qq}',manager={x}, login_count={y},last_login_time='{ DateTime.Now}',type={z},birthday='{birthday}' where id={id}");
            string conn = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
            SqlConnection conn1 = new SqlConnection(conn);
            SqlCommand cmd = new SqlCommand(sql, conn1);
            conn1.Open();
            int flag = cmd.ExecuteNonQuery();
            conn1.Close();
            if (flag > 0)
            {
                DataSet ds = DbHelper.Query($@"select u.*,c.CityName as city_name  from users u inner join S_City c on c.CityID=u.city_id
                                           where u.account = '{name}' and u.pwd = '{pwd}'");
               
                Session["User"] = ds.Tables[0].Rows[0];
                Response.Write("<script>alert('修改成功');window.location.href='Index.aspx'</script>");
             
            }
        }
    }
}